package com.wcfsy.wechat.wx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.wcfsy.wechat.wx.entity.Course;
import com.wcfsy.wechat.wx.pagination.IPage;
import com.wcfsy.wechat.wx.pagination.impl.Page;
import com.wcfsy.wechat.wx.sys.dao.CourseDao;
import com.wcfsy.wechat.wx.util.DateStyle;
import com.wcfsy.wechat.wx.util.DateUtil;
import com.wcfsy.wechat.wx.util.FileUploadUtil;
import com.wcfsy.wechat.wx.util.JsonMessage;

@Controller
@RequestMapping("/cms/course")
public class CourseController {

	@Resource
	private CourseDao courseDao;

	@RequestMapping({ "/view/page" })
	public String page() {
		return "course/list";
	}

	@RequestMapping({ "/view" })
	@ResponseBody
	public IPage<Course> view(int page, int rows) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("index", page);
		paramMap.put("size", rows);
		paramMap.put("limit", "limit");
		return new Page<Course>(courseDao.findByParam(paramMap),
				courseDao.count(paramMap), page, rows);
	}

	@RequestMapping({ "/add/page" })
	public String preAdd() {
		return "course/add";
	}

	@RequestMapping({ "/add" })
	@ResponseBody
	public JsonMessage add(Course info, HttpServletRequest request) {
		try {
			info.setCreateDate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
			info.setStats(1);
			courseDao.add(info);
			return JsonMessage.getSuccessJson("添加成功");
		} catch (Exception e) {
			return JsonMessage.getSuccessJson("添加失败");
		}
	}

	@RequestMapping({ "/delete" })
	@ResponseBody
	public JsonMessage delete(int id) {
		try {
			courseDao.deleteById(id);
			return JsonMessage.getSuccessJson("删除成功");
		} catch (Exception e) {
			return JsonMessage.getSuccessJson("删除失败");
		}

	}

	@RequestMapping({ "/modify/page" })
	public ModelAndView preModify(int id) {
		ModelAndView mav = new ModelAndView("course/modify");
		mav.addObject("data", courseDao.getById(id));
		return mav;
	}

	@RequestMapping({ "/modify" })
	@ResponseBody
	public JsonMessage modify(Course info, HttpServletRequest request) {
		try {
			info.setStats(1);
			courseDao.update(info);
			return JsonMessage.getSuccessJson("修改成功");
		} catch (Exception e) {
			return JsonMessage.getSuccessJson("修改失败");
		}
	}

	@RequestMapping({ "/add/view" })
	public ModelAndView preView(Course info) {
		ModelAndView mav = new ModelAndView("course/view");
		mav.addObject("data", info);
		return mav;
	}
}
